package com.gopay.common.trace.helper;

import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.trace.TraceLog;
import com.gopay.common.trace.TraceLogUtils;
import com.gopay.common.trace.anotation.GopayTracer;
import com.gopay.common.trace.aop.MainOrderLog;
import com.gopay.common.trace.dto.BisBankOrderLog;
import com.gopay.common.trace.enums.TraceType;
import com.gopay.common.trace.util.GopayTracerUtil;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.BeanUtils;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.lang.reflect.Method;

/**
 * 追踪订单helper类，兼容cpsGenMainOrder和bisBankMainOrder
 * Created by zyt on 2017/11/30.
 */
public class LogAllOrderAopHelper extends LogAbstractHelper {


    @Override
    public void before(Object... args) {

    }

    @Override
    public void after(Object... param) {

        TraceType traceType = null;
        try {
            ProceedingJoinPoint joinPoint = (ProceedingJoinPoint) param[0];
            GopayTracer gopayTracer = (GopayTracer) param[1];

            Object fieldValue = GopayTracerUtil.getSpelValue(gopayTracer.paramKey(), joinPoint);

            if(fieldValue instanceof CpsGenMainOrder){
                handleCpsMainOrderInAfter((CpsGenMainOrder)fieldValue, joinPoint);
            } else if(fieldValue instanceof BankOrder){
                handleBisMainOrderInAfter((BankOrder)fieldValue, joinPoint);
            }
        } catch (Exception e) {
            printSelfException(e, "order aop after 阶段自身异常！");
        } finally {

        }
    }

    @Override
    public void ex(Throwable throwable) {
        commonProcessEx(throwable);
    }

    @Override
    public TraceType getTraceType() {
        return null;
    }

    /**
     * 处理cpsGenMainOrder
     * @param cpsGenMainOrder
     * @param joinPoint
     * @throws Exception
     */
    private void handleCpsMainOrderInAfter(CpsGenMainOrder cpsGenMainOrder, ProceedingJoinPoint joinPoint) throws Exception{
        MainOrderLog mainOrderLog = new MainOrderLog();
        TraceType traceType = null;
        convert(cpsGenMainOrder,mainOrderLog);
        String method = joinPoint.getSignature().getName();
        if("save".equals(method)){
            traceType = TraceType.ORDER_SAVE; //save
        } else if("update".equals(method)){
            traceType = TraceType.ORDER_UPDATE; //update
        }
        if (traceType != null) {
            TraceLogUtils.log(new TraceLog(traceType, System.currentTimeMillis() - startTime, mainOrderLog));
        }
    }

    /**
     * 处理bisBankMainOrder
     * @param bankOrder
     * @param joinPoint
     * @throws Exception
     */
    private void handleBisMainOrderInAfter(BankOrder bankOrder, ProceedingJoinPoint joinPoint) throws Exception{
        BisBankOrderLog bisBankOrderLog = new BisBankOrderLog();
        TraceType traceType = null;
//        convert(cpsGenMainOrder,mainOrderLog);
        BeanUtils.copyProperties(bankOrder, bisBankOrderLog);
        bisBankOrderLog.setTid(logContext.getTid());
        String method = joinPoint.getSignature().getName();
        if(StringUtils.containsIgnoreCase(method, "add")){
            traceType = TraceType.BISORDER_SAVE; //save
        } else if(StringUtils.containsIgnoreCase(method, "update")){
            traceType = TraceType.BISORDER_UPDATE; //update
        }
        if (traceType != null) {
            TraceLogUtils.log(new TraceLog(traceType, System.currentTimeMillis() - startTime, bisBankOrderLog));
        }
    }

    private void convert(CpsGenMainOrder cpsGenMainOrder, MainOrderLog mainOrderLog) {
        mainOrderLog.setOrderType(cpsGenMainOrder.getOrderType());
        mainOrderLog.setGopayOrderId(cpsGenMainOrder.getGopayOrderId());
        mainOrderLog.setTransDtlSq(cpsGenMainOrder.getTransDtlSq());
        mainOrderLog.setGopayIntTxnCd(cpsGenMainOrder.getGopayIntTxnCd());
        mainOrderLog.setMerId(cpsGenMainOrder.getMerId());
        mainOrderLog.setMerOrderId(cpsGenMainOrder.getMerOrderId());
        mainOrderLog.setMerTxnTm(cpsGenMainOrder.getMerTxnTm());
        mainOrderLog.setGatewaySource(cpsGenMainOrder.getGatewaySource());
        mainOrderLog.setGatewayChannel(cpsGenMainOrder.getGatewayChannel());
        mainOrderLog.setPayChannel(cpsGenMainOrder.getPayChannel());
        mainOrderLog.setGopayTxnTm(cpsGenMainOrder.getGopayTxnTm());
        mainOrderLog.setTotalOrderAmt(cpsGenMainOrder.getTotalOrderAmt());
        mainOrderLog.setTotalOrderFeeAmt(cpsGenMainOrder.getTotalOrderFeeAmt());
        mainOrderLog.setOrderDealAmt(cpsGenMainOrder.getOrderDealAmt());
        mainOrderLog.setOrderRealFeeAmt(cpsGenMainOrder.getOrderRealFeeAmt());
        mainOrderLog.setMainRecvAcct(cpsGenMainOrder.getMainRecvAcct());
        mainOrderLog.setPayAcct(cpsGenMainOrder.getPayAcct());
        mainOrderLog.setPayCustId(cpsGenMainOrder.getPayCustId());
        mainOrderLog.setTxnStaCd(cpsGenMainOrder.getTxnStaCd());
        mainOrderLog.setOutStlmId(cpsGenMainOrder.getOutStlmId());
        mainOrderLog.setTranFinishTime(cpsGenMainOrder.getTranFinishTime());
        mainOrderLog.setMerTxnAmt(cpsGenMainOrder.getMerTxnAmt());
        mainOrderLog.setAccountPoint(cpsGenMainOrder.getAccountPoint());
        mainOrderLog.setSettleAmt(cpsGenMainOrder.getSettleAmt());
        mainOrderLog.setTid(logContext.getTid());
        mainOrderLog.setErrCode(cpsGenMainOrder.getErrCode());
        mainOrderLog.setErrMsg(cpsGenMainOrder.getErrMsg());
    }

}
